import time
from public.action import *
from public.business import *
from public.testData import *
from public.tools import *

# 1.在首页搜索"小米",点击搜索按钮
# 2.选择小米mix3,加入到购物车,点击购物车按钮
# 3.点击"去结算"按钮
# 4.默认地址已经选择,点击下一步按钮
# 5.支付方式选择"余额支付",点击确认订单
# 6.点击"查看订单详情",点击"取消订单"

testCase = "DBshop004_取消订单"
# 1.在首页搜索"小米",点击搜索按钮
firefox = openBrowser(url)
# 登录
DBshopLogin(firefox,userName,password)
#在首页的搜索框输入小米
firefox.find_element_by_xpath('//input[@name="keywords"]').send_keys('小米')
#点击搜索按钮
firefox.find_element_by_xpath('//input[@type="submit"]').click()
firefox.find_element_by_xpath('//a[@href="/dbshop/goods/8/2"]').click()
#调用函数切换页面
switchWindow(firefox,2)
# 点击添加购物车按钮
firefox.find_element_by_id('add_cart_submit').click()
# 点击去购物车结算
firefox.find_element_by_link_text('去购物车结算').click()
# 点击去结算
firefox.find_element_by_xpath('//a[@class="btn btn-primary btn-next"]').click()
# 点击保存修改
firefox.find_element_by_xpath('//input[@class="btn btn-large btn-primary"]').click()
# 选择余额支付
firefox.find_element_by_xpath('//input[@value="yezf"]').click()
# 确认订单
firefox.find_element_by_xpath('//input[@value="确认订单"]').click()
#使用变量orderNO存储订单编号
orderID=firefox.find_element_by_xpath('(//font[@color="color:#ED145B"])[2]').text
rootPath=get_pro_rootpath()
writeFile(rootPath+r'\public\orderID.txt',orderID) # 将订单编号写入到文件中
firefox.find_element_by_xpath('//input[@value="查看订单详情"]').click()

# 6.点击"查看订单详情",点击"取消订单"
firefox.find_element_by_xpath('//a[contains(text(),"有效订单")]').click()
firefox.find_element_by_name('search_order_sn').send_keys(orderID)
firefox.find_element_by_xpath('//button').click()
firefox.find_element_by_xpath('//button[text()="取消订单"]').click()
firefox.switch_to.alert.accept()

# 断言
firefox.find_element_by_xpath('//a[contains(text(),"无效订单")]').click()
try:
    firefox.find_element_by_link_text(orderID).is_displayed()
    writeLog("%s 测试通过"%testCase)
except:
    writeLog("%s 测试失败" % testCase)
    getPhoto(firefox)
firefox.quit()


